package com.taobao.snowsnow.web;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.taobao.snowsnow.hibernate.pojo.BowParam;
import com.taobao.snowsnow.hibernate.pojo.ProductCollect;
import com.taobao.snowsnow.hibernate.service.BowParamService;
import com.taobao.snowsnow.hibernate.service.ProductCollectService;

public class BowParamAction extends ActionSupport {
	private static final long serialVersionUID = 3127835651449266916L;
	
	private BowParamService bowParamService;
	private ProductCollectService productCollectService;
	
	private Integer productCollectId;
	private Integer bowParamId;
	
	private int[] ids;
	private String[] names;
	private float[] length;
	private int[] index;
	
	private InputStream inputStream;
	
	public String addBowParam() throws UnsupportedEncodingException {
		
		try {
			BowParam param = null;
			ProductCollect p = productCollectService.findById(productCollectId);
			
			for(int i = 0; i < names.length; i++) {
				
				if(ids != null && ids.length >= i) {
					param = bowParamService.findById(ids[i]);
				} else
					param = new BowParam();
				
				param.setLength(length[i]);
				param.setName(new String(names[i].getBytes("utf8"), "utf8"));
				param.setWeight(index[i]);
				param.setProductCollect(p);
				bowParamService.saveOrUpdate(param);
			}
			
			inputStream = new ByteArrayInputStream("成功".getBytes("utf8"));
		} catch (Exception e) {
			inputStream = new ByteArrayInputStream(new String("error: " + e.toString()).getBytes());
			return SUCCESS;
		}
		
		ActionContext.getContext().getSession().put("productCollectId", productCollectId);
		
		return SUCCESS;
	}
	
	public String deleteBowParam() {
		try {
			BowParam param = bowParamService.findById(bowParamId);
			bowParamService.remove(param);
			inputStream = new ByteArrayInputStream("成功".getBytes("utf8"));
		} catch (Exception e) {
			inputStream = new ByteArrayInputStream(new String("error: " + e.toString()).getBytes());
			return SUCCESS;
		}
		return SUCCESS;
	}
	
	public String editBowParam() {
		ProductCollect p = productCollectService.findById(productCollectId);
		List<BowParam> params = null;
		
		try {
			 params = bowParamService.findByProductCollect(p);
		} catch (Exception e) {
			inputStream = new ByteArrayInputStream(new String("error: " + e.toString()).getBytes());
			return SUCCESS;
		}
		
		ActionContext.getContext().getSession().put("productCollectId", productCollectId);
		
		if(params.size() != 0)		
			ActionContext .getContext().getSession().put("bowParams", params);
		return SUCCESS;
	}
	
	public String showBowParam() {
		List<BowParam> bowParams = null;
		
		if(productCollectId != null) {
			try {
				ProductCollect p = productCollectService.findById(productCollectId);
				bowParams = bowParamService.findByProductCollect(p);
				
				sortListByWeight(bowParams);
				ActionContext .getContext().getSession().put("bowParams", bowParams);
			} catch (Exception e) {
				inputStream = new ByteArrayInputStream(new String("error: " + e.toString()).getBytes());
				return SUCCESS;
			}
		} else {
			ActionContext.getContext().getSession().clear();
			ActionContext .getContext().getSession().put("productCollectId", productCollectId);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 冒泡法排序订单
	 * @param list
	 */
	public void sortListByWeight(List<BowParam> list) {
		for(int i = 1; i < list.size(); i++) {
			 for(int j =0; j < list.size() - i; j++) {
				 int newWeight, olderWeight;
				 
				 olderWeight = list.get(j).getWeight();
				 newWeight = list.get(j + 1).getWeight();
				 
				 if(olderWeight > newWeight) {
					 BowParam temp;
					 temp = list.get(j);
					 list.set(j, list.get(j + 1));
					 list.set(j + 1, temp);
				 }
			 }
		 }
	}

	public Integer getProductCollectId() {
		return productCollectId;
	}

	public void setProductCollectId(Integer productCollectId) {
		this.productCollectId = productCollectId;
	}

	public float[] getLength() {
		return length;
	}

	public void setLength(float[] length) {
		this.length = length;
	}

	public String[] getNames() {
		return names;
	}

	public void setNames(String[] names) {
		this.names = names;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public int[] getIndex() {
		return index;
	}

	public void setIndex(int[] index) {
		this.index = index;
	}

	public BowParamService getBowParamService() {
		return bowParamService;
	}

	public void setBowParamService(BowParamService bowParamService) {
		this.bowParamService = bowParamService;
	}

	public ProductCollectService getProductCollectService() {
		return productCollectService;
	}

	public void setProductCollectService(ProductCollectService productCollectService) {
		this.productCollectService = productCollectService;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public Integer getBowParamId() {
		return bowParamId;
	}

	public void setBowParamId(Integer bowParamId) {
		this.bowParamId = bowParamId;
	}
}
